var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bargain_buy_title: '',//砍价按钮文案
    draw_btn_title:'',//1分抽奖
    isLoading: 1,//是否显示加载中图标
    is_bargain: 0,//判断是否为砍价商品
    is_draw:0,//判断是否为抽奖商品
    carousel_is_show: 0,//
    subject_id: 0,//专题ID
    goodslist: [],//商品列表
    page: 1,//初始化商品列表页码
    displayAuthShow: "none",
    goodsNum: 1,//用户选择商品的数量
    index: 1,//存储顶部轮播图的索引
    sourceUri: '',//静态资源地址
    filesSourceUri: '',//普通资源文件地址
    localSourceUri: '',//服务器目录存放地址
    carousel_adv_info: [],//轮播图列表
  },

  // 免费领按钮
  freeReceive: function (e) {
    // console.log(e);
    var _this = this;
    var theGoodsId = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + theGoodsId,
    })
  },
  // 获取专题列表接口
  getGoodsList: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type,
      subject_id: _this.data.subject_id,
      page: _this.data.page
    }
    app.post('/Subject/index', requestData, function (res) {
      if (res.code == 2000) {
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
        for (var i = 0; i < res.data.list.length; i++) {
          res.data.list[i].img = _this.data.filesSourceUri + '/' + res.data.list[i].img;
        }
        var goodslist = _this.data.goodslist;
        goodslist = goodslist.concat(res.data.list);
        _this.setData({
          isLoading: 0,
          is_bargain: res.data.is_bargain,
          is_draw: res.data.is_draw,
          carousel_adv_info: res.data.carousel_img,
          carousel_is_show: res.data.carousel_is_show,
          page: res.data.page,
          goodslist: goodslist
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 获取公共配置文件
  getInitConfig: function () {
    var _this = this;
    var requestData = {
      token: _this.data.token,
      version: app.setConfig.version,
      type: app.setConfig.type
    }
    app.post('/GlobalConfig/getInitConfig', requestData, function (res) {
      if (res.code == 2000) {
        wx.setStorageSync('configs', res.data.configs);
        console.log(res.data.configs)
        _this.setData({
          adv_info: res.data.adv_info,
          adv_solt: res.data.adv_solt,
          robot_list: res.data.robot_list,
          sourceUri: res.data.static_url,
          filesSourceUri: res.data.configs.static_url_http,
          localSourceUri: res.data.configs.static_url_http,
          configs: res.data.configs,
          bargain_buy_title: res.data.configs.bargain_buy_title,
          draw_btn_title: res.data.configs.draw_btn_title
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 顶部机器人轮播结束执行的函数
  animationfinish: function () {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    console.log(options);
    var configs = wx.getStorageSync('configs');
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          sourceUri: '',//静态资源地址
          filesSourceUri: configs.static_url_http,//普通资源文件地址
          localSourceUri: configs.static_url_http,//服务器目录存放地址
          subject_id: options.subject_id,
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          bargain_buy_title: configs.bargain_buy_title,
          draw_btn_title: configs.draw_btn_title
        })
      }
    });
    _this.loop();
  },
  // 判断是否获取到token的值了
  loop: function () {
    var _this = this;
    var theToken = wx.getStorageSync("token");
    if (!theToken) {
      setTimeout(function () { _this.loop() }, 100);
    } else {
      _this.setData({
        token: theToken
      });
      _this.getGoodsList();
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var _this = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    _this.setData({
      goodslist: [],//存储首页书籍列表
      page: 1
    })
    _this.getGoodsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;
    _this.getGoodsList();
  },
  /*角色权限控制*/
  getUserInfo: function (e) {
    var that = this;
    var infoUser = e.detail.userInfo;
    var res = e.detail;
    var url = app.setConfig.url + '/Login/doLogin';
    var data = {
      user_name: infoUser.nickName,
      nick_name: infoUser.nickName,
      head_img: infoUser.avatarUrl,
      sex: infoUser.gender,
      coutry: infoUser.country,
      city: infoUser.city,
      province: infoUser.province,
      code: app.globalData.code,
      encryptedData: res.encryptedData,
      signature: res.signature,
      ctype: app.setConfig.type,
      version: app.setConfig.version,
      iv: res.iv,
    }
    app.postLogin(url, data, function () {
      that.setData({
        displayAuthShow: "none"
      });
      that.loop();
    });
  },
  authRest: function () {
    this.setData({
      displayAuthShow: "none"
    });
    setTimeout(function () {
      wx.reLaunch({
        url: '/pages/home/home',
      })
    }, 1000)
  },

  // 点击顶部导航跳转广告详情
  jumpDetail: function (e) {
    var advType = e.currentTarget.dataset.type_detail;
    console.log(advType);
    if (advType.adv_type == 3) {
      wx.navigateToMiniProgram({
        appId: advType.app_id.toString(),
        path: advType.page.toString(),
        extraData: advType.sence.toString(),
        complete(res) {
          console.log(res)
        }
      });
    } else if (advType.adv_type == 4) {
      wx.navigateTo({
        url: '/' + advType.adv_uri,
      })
    }
  },
  /*红包广告跳转事件*/
  goAdvEvent: function (e) {
    var _this = this;
    var adv_solt = _this.data.adv_solt;
    if (_this.data.adv_solt) {
      //广告数据不为空
      if (adv_solt.adv_type == 1) {
        //图片类
        wx.navigateTo({
          url: '/pages/rule/rule?url=' + adv_solt.adv_info_url
        });
      } else if (adv_solt.adv_type == 3) {
        //小程序类
        wx.navigateToMiniProgram({
          appId: adv_solt.app_id.toString(),
          path: adv_solt.page.toString(),
          extraData: adv_solt.sence.toString(),
          complete(res) {
            console.log(res)
          }
        });
      } else {
        //内部网页类型
        wx.navigateTo({
          url: "/" + adv_solt.adv_uri,
        })
      }
    }
  }
})